package flyingpig.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.extended.ISO8601DateConverter;

import flyingpig.data.dao.SearchJobHome;
import flyingpig.data.domain.SearchJob;




public class RequestSearchJobs extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RequestSearchJobs() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String stMaxJobs = request.getParameter("maxJobs");
		int maxJobs = 10;
		try{
			maxJobs = Integer.parseInt(stMaxJobs);
		}
		catch(Exception e){
			// do nothing
		}
		
		if(maxJobs <= 0) maxJobs = 1;
		
		SearchJobHome dao = new SearchJobHome();
		XStream xs = new XStream();
		xs.alias("SearchJob", SearchJob.class);
		List<SearchJob> searchJobs = dao.findSearchJobs(maxJobs);
		for (int i = 0; i < searchJobs.size(); i++) {
			// Xstream xml			
			xs.toXML(searchJobs.get(i), response
					.getOutputStream());
			if (i != searchJobs.size() - 1) {
				response.getOutputStream().println();
				response.getOutputStream().println("<Seperator/>");
			}
		}

		response.getOutputStream().flush();
		response.getOutputStream().close();
	}
}
